import { fetchLogin } from '@/api'

export default {
    namespaced: true,
    state: {
        token: localStorage.getItem('token')
    },
    mutations: {
        updateToken(state, token) {
            state.token = token
        }
    },
    actions: {
        login({commit}, data) {
            return new Promise(resolve => {
                fetchLogin(data).then(res => {
                    commit('updateToken', res.data.token)
                    if(res.data.token){
                        localStorage.setItem('token', res.data.token)
                    }
                    resolve(res)
                })
            })
        }
    }
}